package com.mdl.jedis;

/**
 * @Desc
 * @Author madengling
 * @Time 2020/12/14 15:34
 */
public class Biji {
/*

Redis的数据结构
--key定义的注意点：
1.不要过长
2.不要过短
3.统一的命名规范

--存储String
1.二进制安全的，存入和获取的数据相同
2.value最多可以容纳的数据长度是512M

--存储String常用命令：
1.赋值2.取值3.删除4.数值增减5.扩展命令
============redis-cli=============
[root@localhost redis]# ./bin/redis-cli
127.0.0.1:6379> set company mdl
OK
127.0.0.1:6379> get company
"mdl"
127.0.0.1:6379> getset company baidu
"mdl"
127.0.0.1:6379> get company
"baidu"
127.0.0.1:6379> set person jack
OK
127.0.0.1:6379> get person
"jack"
127.0.0.1:6379> del person
(integer) 1
127.0.0.1:6379> get person
(nil)
127.0.0.1:6379> incr num
(integer) 1
127.0.0.1:6379> get num
"1"
127.0.0.1:6379> incr num
(integer) 2
127.0.0.1:6379> get num
"2"
127.0.0.1:6379> incr company
(error) ERR value is not an integer or out of range
127.0.0.1:6379> decr num
(integer) 1
127.0.0.1:6379> get num
"1"
127.0.0.1:6379> decr num2
(integer) -1
127.0.0.1:6379> incrby num 5
(integer) 6
127.0.0.1:6379> incrby num3 5
(integer) 5
127.0.0.1:6379> decrby num 3
(integer) 3
127.0.0.1:6379> decrby num4 3
(integer) -3
127.0.0.1:6379> append num 5
(integer) 2
127.0.0.1:6379> get num
"35"
127.0.0.1:6379> append num5 123
(integer) 3
127.0.0.1:6379> get num5
"123"
127.0.0.1:6379>
============redis-cli=============

--存储Hash
1.String Key和String Value的map容器
2.每一个Hash可以存储4294967295个键值对

--存储Hash常用命令：
1.赋值2.取值3.删除4.增加数字5.自学命令
============redis-cli=============
127.0.0.1:6379> hset myhash username jack
(integer) 1
127.0.0.1:6379> hset myhash age 18
(integer) 1
127.0.0.1:6379> hmset myhash2 username rose age 21
OK
127.0.0.1:6379> hget myhash username
"jack"
127.0.0.1:6379> hmget myhash username age
1) "jack"
2) "18"
127.0.0.1:6379> hgetall myhash
1) "username"
2) "jack"
3) "age"
4) "18"
127.0.0.1:6379> hdel myhash2 username age
(integer) 2
127.0.0.1:6379> hgetall myhash2
(empty list or set)
127.0.0.1:6379> hdel myhash2 username
(integer) 0
127.0.0.1:6379> hmset myhash2 username rose age 21
OK
127.0.0.1:6379> del myhash2
(integer) 1
127.0.0.1:6379> hget myhash2 username
(nil)
127.0.0.1:6379> hget myhash age
"18"
127.0.0.1:6379> hincrby myhash age 5
(integer) 23
127.0.0.1:6379> hget myhash age
"23"
127.0.0.1:6379> hexists myhash username
(integer) 1
127.0.0.1:6379> hexists myhash password
(integer) 0
127.0.0.1:6379> hgetall myhash
1) "username"
2) "jack"
3) "age"
4) "23"
127.0.0.1:6379> hlen myhash
(integer) 2
127.0.0.1:6379> hkeys myhash
1) "username"
2) "age"
127.0.0.1:6379> hvals myhash
1) "jack"
2) "23"
127.0.0.1:6379>
============redis-cli=============

--存储list:
1.ArrayList使用数组方式
2.LinkedList使用双向l链接方式
3.双向链表中增加数据
4.双向链表中删除数据

--存储list常用命令：
1.两端添加2.查看列表3.两端弹出4.获取列表元素个数5.扩展命令
============redis-cli=============
127.0.0.1:6379> lpush mylist a b c
(integer) 3
127.0.0.1:6379> lpush mylist 1 2 3
(integer) 6
127.0.0.1:6379> rpush mylist2 a b c
(integer) 3
127.0.0.1:6379> rpush mylist2 1 2 3
(integer) 6
127.0.0.1:6379> lrange mylist 0 5
1) "3"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> lrange mylist2 0 -1
1) "a"
2) "b"
3) "c"
4) "1"
5) "2"
6) "3"
127.0.0.1:6379> lrange mylist2 0 -2
1) "a"
2) "b"
3) "c"
4) "1"
5) "2"
127.0.0.1:6379> lpop mylist
"3"
127.0.0.1:6379> lrange mylist 0 -1
1) "2"
2) "1"
3) "c"
4) "b"
5) "a"
127.0.0.1:6379> rpop mylist2
"3"
127.0.0.1:6379> lrange mylist2 0 -1
1) "a"
2) "b"
3) "c"
4) "1"
5) "2"
127.0.0.1:6379> llen mylist
(integer) 5
127.0.0.1:6379> llen mylist2
(integer) 5
127.0.0.1:6379> llen mylist3
(integer) 0
127.0.0.1:6379> lpushx mylist x
(integer) 6
127.0.0.1:6379> lrange mylist 0 -1
1) "x"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> lpushx mylist3 x
(integer) 0
127.0.0.1:6379> rpush mylist2 y
(integer) 6
127.0.0.1:6379> lrange mylist2 0 -1
1) "a"
2) "b"
3) "c"
4) "1"
5) "2"
6) "y"
127.0.0.1:6379> lpush mylist3 1 2 3
(integer) 3
127.0.0.1:6379> lpush mylist3 1 2 3
(integer) 6
127.0.0.1:6379> lpush mylist3 1 2 3
(integer) 9
127.0.0.1:6379> lrange mylist3 0 -1
1) "3"
2) "2"
3) "1"
4) "3"
5) "2"
6) "1"
7) "3"
8) "2"
9) "1"
127.0.0.1:6379> lrem mylist3 2 3
(integer) 2
127.0.0.1:6379> lrange mylist3 0 -1
1) "2"
2) "1"
3) "2"
4) "1"
5) "3"
6) "2"
7) "1"
127.0.0.1:6379> lrem mylist3 -2 1
(integer) 2
127.0.0.1:6379> lrange mylist3 0 -1
1) "2"
2) "1"
3) "2"
4) "3"
5) "2"
127.0.0.1:6379> lrem mylist3 0 2
(integer) 3
127.0.0.1:6379> lrange mylist3 0 -1
1) "1"
2) "3"
127.0.0.1:6379>
127.0.0.1:6379> lrange mylist 0 -1
1) "x"
2) "2"
3) "1"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> lset mylist 3 mmm
OK
127.0.0.1:6379> lrange mylist 0 -1
1) "x"
2) "2"
3) "1"
4) "mmm"
5) "b"
6) "a"
127.0.0.1:6379> lpush mylist4 a b c
(integer) 3
127.0.0.1:6379> lpush mylist4 a b c
(integer) 6
127.0.0.1:6379> lrange mylist4 0 -1
1) "c"
2) "b"
3) "a"
4) "c"
5) "b"
6) "a"
127.0.0.1:6379> linsert mylist4 before b 11
(integer) 7
127.0.0.1:6379> lrange mylist4 0 -1
1) "c"
2) "11"
3) "b"
4) "a"
5) "c"
6) "b"
7) "a"
127.0.0.1:6379> linsert mylist4 after b 22
(integer) 8
127.0.0.1:6379> lrange mylist4 0 -1
1) "c"
2) "11"
3) "b"
4) "22"
5) "a"
6) "c"
7) "b"
8) "a"
127.0.0.1:6379> lpush mylist5 1 2 3
(integer) 3
127.0.0.1:6379> lpush mylist6 a b c
(integer) 3
127.0.0.1:6379> rpoplpush mylist5 mylist6
"1"
127.0.0.1:6379> lrange mylist5 0 -1
1) "3"
2) "2"
127.0.0.1:6379> lrange mylist6 0 -1
1) "1"
2) "c"
3) "b"
4) "a"
127.0.0.1:6379>
============redis-cli=============

--rpoplpush使用场景
                   消息发布系统mylist
                         ｜
         --------------------------------
        ｜                               ｜
达到设置的条件time>=12                 未达到条件
        ｜                               ｜
满足条件的成功消息       未达到条件，在原有队列中继续循环，只有达到条件后才能弹栈

--存储Set
1.和list类型不同的是，Set集合中不允许出现重复的元素
2.Set可包含的最大元素数量是4294967295

--存储set常用的命令：
1.添加/删除元素2.获得集合中的元素3.集合中的差集运算4.集合中的交集运算5.集合中的并集运算6.扩展命令
============redis-cli=============
127.0.0.1:6379> sadd myset a b c
(integer) 3
127.0.0.1:6379> sadd myset a
(integer) 0
127.0.0.1:6379> sadd myset 1 2 3
(integer) 3
127.0.0.1:6379> srem myset 1 2
(integer) 2
127.0.0.1:6379> smembers myset
1) "b"
2) "c"
3) "a"
4) "3"
127.0.0.1:6379> smembers myset
1) "b"
2) "c"
3) "a"
4) "3"
127.0.0.1:6379> sismember myset a
(integer) 1
127.0.0.1:6379> sismember myset x
(integer) 0
127.0.0.1:6379> sadd mya1 a b c
(integer) 3
127.0.0.1:6379> sadd myb1 a c 1 2
(integer) 4
127.0.0.1:6379> sdiff mya1 myb1
1) "b"
127.0.0.1:6379> sinter mya1 myb1
1) "c"
2) "a"
127.0.0.1:6379> sunion mya1 myb1
1) "b"
2) "c"
3) "1"
4) "a"
5) "2"
127.0.0.1:6379> smembers myset
1) "b"
2) "c"
3) "a"
4) "3"
127.0.0.1:6379> scard myset
(integer) 4
127.0.0.1:6379> srandmember myset
"3"
127.0.0.1:6379> srandmember myset
"b"
127.0.0.1:6379> sdiffstore my1 mya1 myb1
(integer) 1
127.0.0.1:6379> smembers my1
1) "b"
127.0.0.1:6379> sinterstore my2 mya1 myb1
(integer) 2
127.0.0.1:6379> smembers my2
1) "c"
2) "a"
127.0.0.1:6379> sunionstore my3 mya1 myb1
(integer) 5
127.0.0.1:6379> smembers my3
1) "b"
2) "c"
3) "1"
4) "a"
5) "2"
127.0.0.1:6379>
============redis-cli=============

--存储Set使用场景
1.跟踪一些唯一性数据
2.用于维护数据对象之间的关联关系

--存储Sorted-Set
1.Sorted-Set和Set的区别
2.Sorted-Set中的成员在集合中的位置是有序的

--存储Sorted-Set常用命令：
1.添加元素2.获得元素3.删除元素4.范围查询5.扩展命令
============redis-cli=============
127.0.0.1:6379> zadd mysort 70 zs 80 ls 90 ww
(integer) 3
127.0.0.1:6379> zadd mysort 100 zs
(integer) 0
127.0.0.1:6379> zadd mysort 60 tom
(integer) 1
127.0.0.1:6379> zscore mysort zs
"100"
127.0.0.1:6379> zcard mysort
(integer) 4
127.0.0.1:6379> zrem mysort tom ww
(integer) 2
127.0.0.1:6379> zcard mysort
(integer) 2
127.0.0.1:6379> zadd mysort 85 jack 95 rose
(integer) 2
127.0.0.1:6379> zrange mysort 0 -1
1) "ls"
2) "jack"
3) "rose"
4) "zs"
127.0.0.1:6379> zrange mysort 0 -1 withscores
1) "ls"
2) "80"
3) "jack"
4) "85"
5) "rose"
6) "95"
7) "zs"
8) "100"
127.0.0.1:6379> zrevrange mysort 0 -1 withscores
1) "zs"
2) "100"
3) "rose"
4) "95"
5) "jack"
6) "85"
7) "ls"
8) "80"
127.0.0.1:6379> zremrangebyrank mysort 0 4
(integer) 4
127.0.0.1:6379> zadd mysort 80 zs 90 ls 100 ws
(integer) 3
127.0.0.1:6379> zremrangebyscore mysort 80 100
(integer) 3
127.0.0.1:6379> zrange mysort 0 -1
(empty list or set)
127.0.0.1:6379> zadd mysort 70 zs 80 ls 90 ww
(integer) 3
127.0.0.1:6379> zrangebyscore mysort 0 100 withscores
1) "zs"
2) "70"
3) "ls"
4) "80"
5) "ww"
6) "90"
127.0.0.1:6379> zrangebyscore mysort 0 100 withscores limit 0 2
1) "zs"
2) "70"
3) "ls"
4) "80"
127.0.0.1:6379> zincrby mysort 3 ls
"83"
127.0.0.1:6379> zscore mysort ls
"83"
127.0.0.1:6379> zcount mysort 80 90
(integer) 2
127.0.0.1:6379>
============redis-cli=============

--Sorted-Set使用场景
1.如大型在线游戏积分排行榜
2.构建索引数据

--Redis的通用命令127.0.0.1:6379> keys *
============redis-cli=============
 1) "num3"
 2) "mylist3"
 3) "my3"
 4) "my1"
 5) "name"
 6) "mysort"
 7) "num5"
 8) "num2"
 9) "myset"
10) "company"
11) "mylist5"
12) "num"
13) "mylist4"
14) "myb1"
15) "mya1"
16) "mylist"
17) "myhash"
18) "mylist2"
19) "my2"
20) "num4"
21) "mylist6"
127.0.0.1:6379> keys my?
1) "my3"
2) "my1"
3) "my2"
127.0.0.1:6379> del my1 my2 my3
(integer) 3
127.0.0.1:6379> exists my1
(integer) 0
127.0.0.1:6379> exists mya1
(integer) 1
127.0.0.1:6379> get company
"baidu"
127.0.0.1:6379> rename company newcompany
OK
127.0.0.1:6379> get company
(nil)
127.0.0.1:6379> get newcompany
"baidu"
127.0.0.1:6379> expire newcompany 1000
(integer) 1
127.0.0.1:6379> ttl newcompany
(integer) 991
127.0.0.1:6379> type newcompany
string
127.0.0.1:6379> type mylist
list
127.0.0.1:6379> type myset
set
127.0.0.1:6379> type myhash
hash
127.0.0.1:6379> type mysort
zset
127.0.0.1:6379>
============redis-cli=============

--Redis特性
1.多数据库
2.Redis事务 multi开启事务 exec提交 discard回滚
============redis-cli=============
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
(empty list or set)
127.0.0.1:6379[1]> select 0
OK
127.0.0.1:6379> keys *
 1) "num3"
 2) "mylist3"
 3) "name"
 4) "mysort"
 5) "newcompany"
 6) "num5"
 7) "num2"
 8) "myset"
 9) "mylist5"
10) "num"
11) "mylist4"
12) "myb1"
13) "mya1"
14) "mylist"
15) "myhash"
16) "mylist2"
17) "num4"
18) "mylist6"
127.0.0.1:6379> move myset 1
(integer) 1
127.0.0.1:6379> select 1
OK
127.0.0.1:6379[1]> keys *
1) "myset"
127.0.0.1:6379[1]> type myset
set
127.0.0.1:6379[1]>
============redis-cli=============

--Redis两种持久化方式
1.RDB方式2.AOF方式
--持久化使用的方式：
1.RDB持久化2.AOF持久化3.无持久化4.同时使用RDB和AOF

 */
}
